package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.CircuitDiagramDTO;
import com.example.entity.CircuitDiagram;
import com.example.mapper.CircuitDiagramMapper;
import com.example.service.CircuitDiagramService;
import com.example.vo.CircuitDiagramVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 一次图结果业务逻辑层实现类
 * 
 * @author system
 * @since 2024-01-01
 */
@Service
@Slf4j
public class CircuitDiagramServiceImpl extends ServiceImpl<CircuitDiagramMapper, CircuitDiagram> 
    implements CircuitDiagramService {
    
    @Override
    public IPage<CircuitDiagramVO> getPage(IPage<CircuitDiagram> page, String name) {
        // 构建查询条件
        LambdaQueryWrapper<CircuitDiagram> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            queryWrapper.like(CircuitDiagram::getName, name);
        }
        queryWrapper.orderByDesc(CircuitDiagram::getCreateTime);
        
        // 执行分页查询
        IPage<CircuitDiagram> resultPage = this.page(page, queryWrapper);
        
        // 转换为VO对象
        IPage<CircuitDiagramVO> voPage = new Page<>(resultPage.getCurrent(), resultPage.getSize(), resultPage.getTotal());
        List<CircuitDiagramVO> voList = resultPage.getRecords().stream()
            .map(this::convertToVO)
            .collect(Collectors.toList());
        voPage.setRecords(voList);
        
        return voPage;
    }
    
    @Override
    public CircuitDiagramVO getDetailById(String id) {
        CircuitDiagram circuitDiagram = this.getById(id);
        if (circuitDiagram == null) {
            return null;
        }
        return convertToVO(circuitDiagram);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCircuitDiagram(CircuitDiagramDTO dto) {
        CircuitDiagram circuitDiagram = new CircuitDiagram();
        BeanUtils.copyProperties(dto, circuitDiagram);
        
        // 设置创建信息
        circuitDiagram.setCreateTime(LocalDateTime.now());
        circuitDiagram.setCreateBy(dto.getOperator());
        circuitDiagram.setUpdateTime(LocalDateTime.now());
        circuitDiagram.setUpdateBy(dto.getOperator());
        
        boolean result = this.save(circuitDiagram);
        if (result) {
            log.info("一次图保存成功，ID：{}，名称：{}", circuitDiagram.getId(), circuitDiagram.getName());
        }
        return result;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCircuitDiagram(CircuitDiagramDTO dto) {
        CircuitDiagram circuitDiagram = this.getById(dto.getId());
        if (circuitDiagram == null) {
            log.error("一次图不存在，ID：{}", dto.getId());
            return false;
        }
        
        BeanUtils.copyProperties(dto, circuitDiagram);
        
        // 设置更新信息
        circuitDiagram.setUpdateTime(LocalDateTime.now());
        circuitDiagram.setUpdateBy(dto.getOperator());
        
        boolean result = this.updateById(circuitDiagram);
        if (result) {
            log.info("一次图更新成功，ID：{}，名称：{}", circuitDiagram.getId(), circuitDiagram.getName());
        }
        return result;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCircuitDiagram(String id) {
        CircuitDiagram circuitDiagram = this.getById(id);
        if (circuitDiagram == null) {
            log.error("一次图不存在，ID：{}", id);
            return false;
        }
        
        boolean result = this.removeById(id);
        if (result) {
            log.info("一次图删除成功，ID：{}，名称：{}", id, circuitDiagram.getName());
        }
        return result;
    }
    
    /**
     * 实体对象转换为VO对象
     * 
     * @param circuitDiagram 一次图实体
     * @return 一次图VO对象
     */
    private CircuitDiagramVO convertToVO(CircuitDiagram circuitDiagram) {
        CircuitDiagramVO vo = new CircuitDiagramVO();
        BeanUtils.copyProperties(circuitDiagram, vo);
        return vo;
    }
} 